import service from './service'
import { CONTENT_TYPE } from '../../constants'
// import qs from 'qs';
import { useUserStoreWithOut } from "@/store/modules/user"

const request = <T>(option: any): Promise<T> => {
  const { url, method, params, data, headers } = option
  const userStore = useUserStoreWithOut()
  return service.request({
    url: url,
    method,
    params,
    data,
    // responseType: responseType,
    headers: {
      'Content-Type': CONTENT_TYPE,
      [userStore.getTokenKey ?? 'Authorization']: userStore.getAccessToken ?? '',
      ...headers
    }
  }) as Promise<T>
}

export default {
  get<T>(url: string, params: any={}, option?: any): Promise<T> {
    return request({ method: 'get', url, params, ...option })
  },
  post<T>(url: string, data: any, option?: any): Promise<T> {
    return request({ method: 'post', url, data, ...option })
  },
  delete<T>(url: string, option?: any): Promise<T> {
    return request({ method: 'delete', url, ...option })
  },
  put<T>(url: string, option?: any): Promise<T> {
    return request({ method: 'put', url, ...option })
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  }
}
